﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Agile.Engine.Utils
{
    public class ReflectUtils
    {
        /// <summary>
        /// 解析类型
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static Type Parse(string type)
        {
            return Spring.Core.TypeResolution.TypeResolutionUtils.ResolveType(type);
        }

        /// <summary>
        /// 反射创建实例
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="type"></param>
        /// <returns></returns>
        public static T Instance<T>()
            where T : class
        {
            var ctor = typeof(T).GetConstructor(Type.EmptyTypes);
            return new Spring.Reflection.Dynamic.SafeConstructor(ctor).Invoke(Type.EmptyTypes) as T;
        }

        public static object Instance(string type)
        {
            var t = Parse(type);
            var ctor = t.GetConstructor(Type.EmptyTypes);
            return new Spring.Reflection.Dynamic.SafeConstructor(ctor).Invoke(Type.EmptyTypes);
        }

        public static object DynamicMethodInvoke(object target, System.Reflection.MethodInfo info, params object[] args)
        {
            Spring.Reflection.Dynamic.SafeMethod method = new Spring.Reflection.Dynamic.SafeMethod(info);
            return method.Invoke(target, args);
        }
    }
}
